package com.example.ivory.accountbook.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.ivory.accountbook.R;
//import com.example.ivory.accountbook.fragment.Fragment_addItem;
import com.example.ivory.accountbook.fragment.Fragment_details;
import com.example.ivory.accountbook.fragment.Fragment_graph;
import com.example.ivory.accountbook.fragment.Fragment_me;
import com.example.ivory.accountbook.fragment.Fragment_wallet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ivory on 2016/8/27.
 */
public class Activity_main extends AppCompatActivity implements ViewPager.OnPageChangeListener,View.OnClickListener{
    private ViewPager viewPager;
    private RadioGroup group;
    private Fragment_details fragment_details;
    private Fragment_wallet fragment_wallet;
    private Fragment_graph fragment_graph;
    private Fragment_me fragment_me;
    FragmentManager manager;

    private RadioButton details,wallet,addItem,graph,me;
    private List<Fragment> fragments;
    private long firstTime = 0;

    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.guide);
        initView();
        initEvent();
    }

    private void initView(){
        fragments=new ArrayList<>();
        manager=getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fragment_details=new Fragment_details();
        fragment_wallet=new Fragment_wallet();
      ///  fragment_addItem=new Fragment_addItem();
        fragment_graph=new Fragment_graph();
        fragment_me=new Fragment_me();

        fragments.add(fragment_details);
        fragments.add(fragment_wallet);
        //fragments.add(fragment_addItem);
        fragments.add(fragment_graph);
        fragments.add(fragment_me);


        details=(RadioButton)findViewById(R.id.details);
        wallet=(RadioButton)findViewById(R.id.wallet);
        addItem=(RadioButton)findViewById(R.id.addItem);
        graph=(RadioButton)findViewById(R.id.graph);
        me=(RadioButton)findViewById(R.id.me);
        group = (RadioGroup) findViewById(R.id.tab_btn_group);

        viewPager = (ViewPager) findViewById(R.id.fragments_container);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            public int getCount() {
                return fragments.size();
            }
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
        });


        viewPager.addOnPageChangeListener(this);

    }

    private void initEvent(){
        details.setOnClickListener(this);
        wallet.setOnClickListener(this);
        addItem.setOnClickListener(this);
        graph.setOnClickListener(this);
        me.setOnClickListener(this);

    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.details:
                viewPager.setCurrentItem(0,true);
                break;
            case R.id.wallet:
                viewPager.setCurrentItem(1,true);
                break;
            case R.id.addItem:
               Intent i =new Intent(Activity_main.this,AddItem.class);
                startActivity(i);
                break;
            case R.id.graph:
                viewPager.setCurrentItem(2,true);
                break;
            case R.id.me:
                viewPager.setCurrentItem(3,true);
                break;
    }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                group.check(R.id.details);
                break;
            case 1:
                group.check(R.id.wallet);
                break;
//            case 2:
//                group.check(R.id.addItem);
//                break;
//            case 2:
//                group.check(R.id.addItem);
//                break;
            case 2:
                group.check(R.id.graph);
                break;
            case 3:
                group.check(R.id.me);
                break;
            default:
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;
        } else {
            finish();
            finish();

        }
    }

}


